﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CRM.Entity;
using CRM.BLL;
using DP.Web.UI;
using DP.Common;
using System.Text;

namespace CRM.Web.SurveyManage
{
    public partial class SurveyPlanModify : Common.PageBase
    {
        T_SURVEY_PLAN_BLL sBll = new T_SURVEY_PLAN_BLL();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            PageHelper.SetPrePageUrl(this.Page);
            PageHelper.SetGoBackUrl(this.Page, btnCancel, StaticConstant.NotBackUrl);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                pageDataInit();
            }
        }
        #region 页面初始化数据绑定
        #endregion

        private void pageDataInit()
        {
            string _autoid = String.IsNullOrEmpty(Request.QueryString["AUTOID"]) ? "" : Request.QueryString["AUTOID"].Trim();
            if (!string.IsNullOrEmpty(_autoid))
            {
                T_SURVEY_PLAN_Entity entity = new T_SURVEY_PLAN_Entity();
                entity = sBll.GetEntity("AUTOID", StringHelper.ToInt(_autoid));
                PageHelper.SetControlValue(this.Page, entity);
                lblTitle.Text = "问卷任务修改";
            }
            else
            {
                txtCREATEPEOPLE.Text = _userLoginInfo.UserEntity.USERNAME.ToString();
                txtSDATE.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                lblTitle.Text = "问卷任务添加";
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            List<T_SURVEY_PLAN_Entity> list = new List<T_SURVEY_PLAN_Entity>();
            T_SURVEY_PLAN_Entity entity = new T_SURVEY_PLAN_Entity();
            if (string.IsNullOrEmpty(this.txtPlan_Name.Text))
            {
                PageHelper.ShowMessage(this.Page, "计划名称不能为空！");
                return;
            }
            if (this.txtTask_Table_Name.Text == "T_SURVEY_TASK_")
            {
                PageHelper.ShowMessage(this.Page, "请输入任务表名称！");
                return;
            }
            else if (this.txtTask_Table_Name.Text.Length < 14)
            {
                PageHelper.ShowMessage(this.Page, "任务表名称必须以T_SURVEY_TASK_打头！");
                return;
            }
            else if (this.txtTask_Table_Name.Text.Substring(0, 14) != "T_SURVEY_TASK_")
            {
                PageHelper.ShowMessage(this.Page, "任务表名称必须以T_SURVEY_TASK_打头！");
                return;
            }
            if (string.IsNullOrEmpty(this.txtSurvey_ID.Text))
            {
                PageHelper.ShowMessage(this.Page, "选择问卷不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(txtAutoid.Text))
            {
                list = sBll.GetList(string.Format(" AND TASK_TABLE_NAME='{0}' AND SURVEY_ID='{1}'", txtTask_Table_Name.Text, txtSurvey_ID.Text));
                if (list.Count > 0)
                {
                    PageHelper.ShowMessage(this.Page, "编号为<" + txtSurvey_ID.Text + ">的问卷已经包含在任务表" + txtTask_Table_Name.Text + "中，请检查！");
                    return;
                }
                entity = new T_SURVEY_PLAN_Entity();
                PageHelper.GetControlValue<T_SURVEY_PLAN_Entity>(this.Page, ref entity);
                entity.GHID = _userLoginInfo.UserEntity.USERID.ToString();
                entity.BUSINESSDIVISIONID = _userLoginInfo.UserEntity.BUSINESSDIVISIONID;
                entity.BUSINESSDIVISIONCODE = _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE;
                entity.BUSINESSDIVISIONNAME = _userLoginInfo.UserEntity.BUSINESSDIVISIONNAME;
                long rel = sBll.Insert(entity);
                if (rel > 0)
                {
                    PageHelper.ShowMessage(this.Page, "添加成功！", "SurveyPlanList.aspx");
                }
            }
            else
            {
                entity = sBll.GetEntity("AUTOID", txtAutoid.Text);
                if (entity == null)
                {
                    entity = new T_SURVEY_PLAN_Entity();
                    PageHelper.GetControlValue<T_SURVEY_PLAN_Entity>(this.Page, ref entity);
                    entity.GHID = _userLoginInfo.UserEntity.USERID.ToString();
                    entity.BUSINESSDIVISIONID = _userLoginInfo.UserEntity.BUSINESSDIVISIONID;
                    entity.BUSINESSDIVISIONCODE = _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE;
                    entity.BUSINESSDIVISIONNAME = _userLoginInfo.UserEntity.BUSINESSDIVISIONNAME;
                    long rel = sBll.Insert(entity);
                    if (rel > 0)
                    {
                        PageHelper.ShowMessage(this.Page, "添加成功！", "SurveyPlanList.aspx");
                    }
                }
                else
                {
                    PageHelper.GetControlValue<T_SURVEY_PLAN_Entity>(this.Page, ref entity);
                    long rel = sBll.Update(entity);
                    PageHelper.ShowMessage(this.Page, "修改成功！", "SurveyPlanList.aspx");
                }
            }
        }
    }
}
